var stationId=ym.getQueryStringByName("stationId");
var vaccineId="";
var orderDay="";//初始日期
var date_value="";//切换后日期
var obj={};
$(function(){
    var dates = new Date();
    var month=dates.getMonth()+1;
    if(month<10){
        month="0"+month;
    }
    var days=dates.getDate();
    if(days<10){
        days="0"+days;
    }
    orderDay=dates.getFullYear()+"-"+month+"-"+days;
    $("#vaccine").select2();
    $("#vaccine").on("select2:select",function(){
       vaccineId = $(this).val();
        //查询剂次
        getAgentTimes(vaccineId);
    });
    getStation();
})
//获取服务站信息
function getStation(){
    ym.ajax({
        url: "/vaccinestation/queryVaccineStation",
        param:{
            stationId:stationId,
        },
        success: function (data) {
            console.log(data)
            console.log("获取服务站信息")
            obj=data.obj.results[0];
            $(".stationName").html(data.obj.results[0].stationName);
            $(".stationAdrr").html("地址："+data.obj.results[0].stationAddr);
            $(".stationTimes").html("服务时间："+data.obj.results[0].stationTime);
            $(".stationNum").html("服务积分："+data.obj.results[0].serviceIntegral+"积分");
            var orderDay=data.obj.results[0].orderDay;
            var nowTemp = new Date();
            var nowDay = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0).valueOf();
            var bigDay=new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate()+5, 0, 0, 0, 0).valueOf();
            var nowMoth = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), 1, 0, 0, 0, 0).valueOf();
            var nowYear = new Date(nowTemp.getFullYear(), 0, 1, 0, 0, 0, 0).valueOf();
            $('#doc-datepicker').datepicker({
                onRender: function(date, viewMode) {
                    // 默认 days 视图，与当前日期比较
                    var viewDate = nowDay;
                    switch (viewMode) {
                        // moths 视图，与当前月份比较
                        case 1:
                            viewDate = nowMoth;
                            break;
                        // years 视图，与当前年份比较
                        case 2:
                            viewDate = nowYear;
                            break;
                    }
                    return date.valueOf() < viewDate||date.valueOf()> bigDay? 'am-disabled' : '';

                }
            }).on('changeDate.datepicker.amui', function(event) {
                var date = new Date(event.date);
                var month=date.getMonth() + 1;
                var day=date.getDate();
                if(month<10){
                    month="0"+month;
                }
                if(day<10){
                    day="0"+day;
                }
                date_value=date.getFullYear() + '-' + month + '-' + day;
            });
        }
    });
    getChild();
}
//获取宝宝信息
function getChild(){
    ym.ajax({
        url: "/userChild/getUserChild",
        success: function (data) {
            console.log(data)
            if(data.res == 1){
                 if(data.obj!=null&&data.obj.length>0){
                     var html="";
                     for(var i=0;i<data.obj.length;i++){
                         var l=data.obj[i];
                        html+="<option value='"+l.childId+"' code='"+l.childCode+"'>"+l.childRealname+"</option>"
                     }
                     $("#childName").append(html);
                 }
            }else{
                console.log("获取宝宝失败!")
            }
        }
    });
    getVaccine()
}
//获取疫苗
function getVaccine(){
    ym.ajax({
        url : "/stationVaccine/getVaccineTypeByStationId",
        type : "get",
        param:{
            "stationId":stationId,
        },
        jsonType : "json",
        success : function(data) {
            console.log(data)
            console.log("获取疫苗")
            if(data.obj!=null&&data.obj.length>0){
                var html="";
                for(var i=0;i<data.obj.length;i++){
                    var l=data.obj[i];
                    html+="<option value='"+l.vaccineTypeId+"'>"+l.vaccineTypeName+"</option>";
                }
                $("#vaccine").append(html);
            }

        }
    })
}
//查询剂次
function getAgentTimes(vaccineId){
    $("#agentNum").html("");
    ym.ajax({
        url : "/stationVaccine/getYmVaccineByVaccineTypeId",
        type : "get",
        param:{
            "vaccineTypeId":vaccineId,
            "childeId":$("#childName option:selected").val(),
        },
        jsonType : "json",
        success : function(data) {
            console.log(data)
            console.log("获取剂次");
            var html="<option value=''>请选择剂次</option>";
            if(data.obj!=null&&data.obj.length>0){
                for(var i=0;i<data.obj.length;i++){
                    var l=data.obj[i];
                    html+="<option value='"+l.vaccineId+"'>第"+l.agent+"剂</option>";
                }
            }
            $("#agentNum").append(html)
        }
    })
}
//立即预约
function sureMark(){
   if($("#vaccine option:selected").val()==""){
       $("#my-modal .am-modal-bd").html("您还未选择疫苗，若无可选择疫苗，此站点暂未开通服务，请重新选择站点！");
       $("#my-modal").modal();
       setTimeClose()
   }else if($("#agentNum option:selected").val()==""){
       $("#my-modal .am-modal-bd").html("请选择所需剂次！");
       $("#my-modal").modal();
       setTimeClose()
   }else{
       $("#myConfirm .am-modal-bd").html("此站点工作日为"+obj.stationTime+"，请核对好预约日期是否在工作日内。预约此疫苗将扣除您"+obj.serviceIntegral+"积分，是否确认立即预约？");
       var $confirm = $('#myConfirm');
       var confirm = $confirm.data('amui.modal');
       var onConfirm = function() {
           var orderTime="";
           if(date_value!=null&&date_value!=undefined&&date_value!=""){
               orderTime=date_value
           }else{
               orderTime=orderDay
           }
           //点击确认按钮调用事件
           ym.ajax({
               url:'/stationVaccine/insertYmOrderVaccination',
               type :"post",
               param:{
                   childId:$("#childName option:selected").val(),
                   stationId:obj.stationId,
                   vaccineId:$("#agentNum option:selected").val(),
                   orderTime:orderTime,
                   point:obj.serviceIntegral,
                   vaccineCard:$("#childName option:selected").attr("code"),
               },
               success:function(data){
                   console.log(data)
                   if(data.res==1){
                       $("#my-modal .am-modal-bd").html("预约成功！");
                       $("#my-modal").modal();
                       setTimeout(function(){
                           $("#my-modal").modal('close');
                           cosnole.log("跳转页面")
                       },2000)
                   }
               }
           });

       };
       var onCancel = function() {
           //取消按钮调用事件
       };
       if (confirm) {
           confirm.options.onConfirm =  onConfirm;
           confirm.options.onCancel =  onCancel;
           confirm.toggle(this);
       } else {//重定义模态窗函数
           $confirm.modal({
               relatedElement: this,
               closeViaDimmer: false,//禁止点击遮罩层关闭
               onConfirm: onConfirm,
               onCancel: onCancel
           });
       }
   }
}
function setTimeClose(){
    setTimeout(function(){
        $("#my-modal").modal('close');
    },2000)
}



